<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>校园生活后端</title>
  <link rel="shortcut icon" href="../../favicon.ico">
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css">
  <link rel="stylesheet" href="../../styles/login.css">
  <link rel="stylesheet" href="../../styles/icon/iconfont.css" />
  <style>
    body{
      min-width: 1360px;
    }
  </style>
  <script src="/static/js/axios.min.js"></script>
</head> 

<body>
  <div class="login" id="login-app">
    <div class="login-box">
      <img src="../../images/login/anniLogin.jpg" alt="">
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" >
          <div class="login-form-title">
            <!--图标部分-->
            <img src="../../images/login/logo.jpg" style="width:100px;height:102px; round-clip: v-bind(10)" alt="" />
          </div>
          <el-form-item prop="id">
            <el-input v-model="loginForm.id" type="text" auto-complete="off" placeholder="账号" maxlength="20" prefix-icon="iconfont icon-member"></el-input>
           </el-form-item>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="用户名" maxlength="20"
              prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="iconfont icon-lock" maxlength="20"
              @keyup.enter.native="handleLogin"></el-input>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button :loading="loading" class="login-btn" size="medium" type="primary" style="width:100%;"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

  <!-- 开发环境版本，包含了有帮助的命令行警告 -->
  <script src="../../plugins/vue/vue.js"></script>
  <!-- 引入组件库 -->
  <script src="../../plugins/element-ui/index.js"></script>
  <!-- 引入axios -->
  <script src="../../plugins/axios/axios.min.js"></script>
  <script src="../../js/request.js"></script>
  <script src="../../js/common.js"></script>
  <script src="../../js/validate.js"></script>
  <script src="../../api/login.js"></script>

  <script>
    new Vue({
      el: '#login-app',
      data() {
        return {
          loginForm:{
            id:'2104040213',
            username: 'Rainy-Heights',
            password: 'password'
          },
          loading: false
        }
      },
      computed: {
        loginRules() {
          const validateUsername = (rule, value, callback) => {
            if (value.length < 1 ) {
              callback(new Error('请输入用户名'))
            } else {
              callback()
            }
          }
          const validatePassword = (rule, value, callback) => {
            if (value.length < 3) {
              callback(new Error('密码必须在3位以上'))
            } else {
              callback()
            }
          }
          const validateId = (rule, value, callback) => {
            if (value.length < 1) {
              callback(new Error('请输入账号'))
            } else {
              callback()
            }
          }
          return {
            'id': [{ 'validator': validateId, 'trigger': 'blur' }],
            'username': [{ 'validator': validateUsername, 'trigger': 'blur' }],
            'password': [{ 'validator': validatePassword, 'trigger': 'blur' }]
          }
        }
      },
      created() {
      },
      methods: {
        async handleLogin() {
          const params = {
            id: this.loginForm.id,
            username: this.loginForm.username,
            password: this.loginForm.password
          }
          this.$refs.loginForm.validate(async (valid) => {
            if (valid) {
              this.loading = true
              let res = await loginApi(params)
              if (String(res.code) === '200') {
                sessionStorage.setItem("token", res.data);
                localStorage.setItem('token',res.data)//登录成功会存储数据到浏览器
                window.location.href= '/index.html'
              } else {
                this.$message.error(res.message)
                this.loading = false
              }
            }
          })
        },
       async handleSignIn(){
         window.parent.menuHandle({
           id: '4',
           url: '/page/goods/add.html',
           name: '注册新用户'
         },true)
       }
      }
    })
  </script>
</body>

</html>
